/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.TimeUnit;

/**
 * <p>Project: app2022 - T20
 * <p>Powered by webrx On 2022-02-07 15:51:17
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T20 implements Runnable {
    private final String name;
    private final Object co;

    public T20(String name, Object o) {
        this.name = name;
        this.co = o;
    }

    public static void main(String[] args) {
        Object co = new Object();
        System.out.println(co);

        for (int i = 0; i < 5; i++) {
            var tt = new T20("Thread" + i, co);
            var t = new Thread(tt);
            t.start();
        }

        try {
            TimeUnit.SECONDS.sleep(2);
            System.out.println("-----Main Thread notify-----");
            synchronized (co) {
                co.notify();
                //co.notifyAll();
            }

            TimeUnit.SECONDS.sleep(2);
            System.out.println("Main Thread is end.");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        System.out.println(name + " is waiting.");
        try {
            synchronized (co) {
                co.wait();
            }
            System.out.println(name + " has been notified.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
